package com.jss.lanouhn.fristapp.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.SelecterDetails;
import com.jss.lanouhn.fristapp.adapters.RvcAdapter;
import com.jss.lanouhn.fristapp.modles.SelecterCommendInfo;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/5.
 */
public class Fragment_Selecter_Comment extends Fragment{
    private String urlTop="http://api.liwushuo.com/v2/items/";
    private String uriEnd="/comments?limit=20&offset=0";
    private String url;
    private RecyclerView rv;
    private SelecterCommendInfo scinfos;
    private RvcAdapter rvcAdapter;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getContext()).inflate(R.layout.fragment_selecter_comment,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rv= (RecyclerView) getView().findViewById(R.id.rv_selecter_comment);
        String id=((SelecterDetails)getActivity()).getId();
        url=urlTop+id+uriEnd;
        initData();

    }

    private void initData() {
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url(url)
                .build();
        Call call=client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result=response.body().string();
                Gson gson=new Gson();
                scinfos=gson.fromJson(result,SelecterCommendInfo.class);
                handler.sendEmptyMessage(1);


            }
        });

    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    rvcAdapter=new RvcAdapter(scinfos,getContext());
                    rv.setAdapter(rvcAdapter);
                    LinearLayoutManager manager=new LinearLayoutManager(getContext());
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    rv.setLayoutManager(manager);
                    break;
            }
        }
    };
}
